package com.csist.servlet;

import com.csist.bean.Page;
import com.csist.bean.Student;
import com.csist.dao.StudentDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SelectStudentServlet", value = {"/SelectStudentServlet"})
public class SelectStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //session过期就回主页面去
        if (request.getSession().getAttribute("teacher")==null){
            response.sendRedirect("login.html");
            return;
        }

        //班级编号
        int cid = Integer.parseInt(request.getParameter("cid"));
        //当前页码
        int curPage = Integer.parseInt(request.getParameter("cpage"));
        StudentDao sDao = new StudentDao();
        //获取数据条数
        int dataNum = sDao.getStudentDataNumByC_id(cid);
        //如果没数据
        if (dataNum==0){
            response.sendRedirect("login.html");
            return;
        }
        //每页至多显示数据条数
        int viewDataNum = 2;
        //总页数
//        int sumPages = dataNum%viewDataNum==0?dataNum/viewDataNum:dataNum/viewDataNum+1;

        Page<Student> sPage = new Page<>(curPage,viewDataNum,dataNum);
        sPage.setDatalist(sDao.getStudentsByPage(cid,curPage,viewDataNum));

        System.out.println(sPage);
        request.setAttribute("pageData",sPage);
        request.setAttribute("cid",cid);
        request.getRequestDispatcher("jsp/StudentMsg.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
